package cn.renxuan.controllers;

import org.springframework.web.bind.annotation.RestController;

import cn.renxuan.CommonUtil;
import cn.renxuan.entity.FS_DomesticWeather;
import cn.renxuan.service.domesticweather.IDomesticWeatherService;
import cn.renxuan.service.weather.IWeatherService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/weather")
public class WeatherController {

    private IWeatherService weatherService;

    private IDomesticWeatherService domesticWeatherService;

    @Autowired
    public WeatherController(IWeatherService weatherService, IDomesticWeatherService domesticWeatherService) {
        this.domesticWeatherService = domesticWeatherService;
        this.weatherService = weatherService;
    }

    /**
     * 添加国内天气服务
     * 
     * @param areaCode 地区编码
     * @return 成功或失败
     */
    @PostMapping("add")
    public boolean addWeatherService(@RequestParam String areaCode) throws Exception {
        return domesticWeatherService.addLundearWeather(areaCode);
    }

    /**
     * 获取实时天气
     * 
     * @param areaCode 地区编码
     * @return 成功或失败
     */
    @GetMapping("getRealTimeWeather")
    public String getWeather(@RequestParam String areaCode) {
        String retVal = null;
        Map<String, String> queryParams = new HashMap<String, String>();
        queryParams.put("areaCode", areaCode);
        try {
            FS_DomesticWeather domesticWeather = weatherService.getDomesticWeather(queryParams, null);
            retVal = "成功";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            CommonUtil.printThreadLog(String.format("获取实时天气失败：%s", e.getMessage()));
            retVal = "失败";
        }
        return retVal;
    }
}
